import { createRouter, createWebHistory, createWebHashHistory } from "vue-router";
// import NProgress from "nprogress";
import login from "../views/login/index.vue";
import home from "../views/home/index.vue";
import ticketDetail from "../views/ticket/ticketDetail.vue";

// import workbench from "../views/workbench.vue";
// import scancode from "../views/scancode.vue";
// import knowledge from "../views/knowledge.vue";
// import myInfo from "../views/myInfo.vue";
// import pointInspection from "../views/pointInspection.vue";
// import ticket from "../views/ticket.vue";
// import equipmentFile from "../views/equipmentFile/index.vue";
import { getToken } from "@/utils/auth";

const pages = import.meta.glob("../views/**/page.js", {
  eager: true,
  import: "default"
});
const components = import.meta.glob("../views/**/index.vue", {});
const filterPaths = ["../views/login/page.js", "../views/home/page.js"];
const routes = Object.entries(pages)
  .map(([path, meta]) => {
    if (filterPaths.includes(path)) {
      return false;
    }
    const compPath = path.replace("/page.js", "/index.vue");
    const component = components[compPath];
    path = path.replace("../views", "").replace("/page.js", "") || "/";
    const name = path.split("/").filter(Boolean).join("-") || "index";
    if (meta.params) {
      path = path + meta.params;
    }
    return {
      path,
      name,
      meta,
      component
    };
  })
  .filter(Boolean);
 

  
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/login",
      name: "login",
      component: login
    },
    {
      path: "",
      redirect: "/workbench"
    },
    {
      path: "",
      name: "home",
      component: home,
      children: [
        ...routes,
        {
          path: "/ticketDetail/:id?",
          name: "ticketDetail",
          meta: {
            title: "我的工单"
          },
          component: ticketDetail
        }
      ]
      // children: [
      //   {
      //     path: "/workbench",
      //     name: "workbench",
      //     meta: {
      //       title: "工作台"
      //     },
      //     component: workbench
      //   },
      //   {
      //     path: "/scancode",
      //     name: "scancode",
      //     meta: {
      //       title: "扫码"
      //     },
      //     component: scancode
      //   },
      //   {
      //     path: "/knowledge",
      //     name: "knowledge",
      //     meta: {
      //       title: "知识库"
      //     },
      //     component: knowledge
      //   },
      //   {
      //     path: "/myinfo",
      //     name: "myInfo",
      //     meta: {
      //       title: "我的"
      //     },
      //     component: myInfo
      //   },
      //   {
      //     path: "/pointInspection",
      //     name: "pointInspection",
      //     meta: {
      //       title: "我的点巡检"
      //     },
      //     component: pointInspection
      //   },
      //   {
      //     path: "/ticket",
      //     name: "ticket",
      //     meta: {
      //       title: "我的工单"
      //     },
      //     component: ticket
      //   },
      //   {
      //     path: "/equipmentFile",
      //     name: "equipmentFile",
      //     meta: {
      //       title: "设备档案"
      //     },
      //     component: equipmentFile
      //   },
      //   {
      //     path: "/inspectionTask/:type/:id?",
      //     name: "inspectionTask",
      //     meta: {
      //       title: "点巡检任务"
      //     },
      //     component: inspectionTask
      //   }
      // ]
    }
  ]
});

// 全局路由守卫
router.beforeEach((to, from, next) => {
  if (getToken()) {
    next();
    return;
  } else {
    next();
    return;  //暂时去掉登录限制
    // if (whiteList.indexOf(to.path) !== -1) {
    //   next();
    //   return;
    // }
    if (to.path == "/login") {
      next();
      return;
    } else {
      next(`/login?redirect=${to.path}`);
    }
  }
});


export default router;
